package com.employeemanage.controller;

import com.employeemanage.domain.bean.TblProductOrder;
import com.employeemanage.domain.resp.Result;
import com.employeemanage.domain.vo.ProductOrderVO;
import com.employeemanage.service.TblProductOrderService;
import com.employeemanage.domain.resp.AjaxResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品销售订单 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/product/order")
public class TblProductOrderController {

    private static final Logger log = LoggerFactory.getLogger(TblProductOrderController.class);

    @Autowired
    private TblProductOrderService orderService;

    // 查询订单列表
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) String orderType,
                             @RequestParam(required = false) String customerName,
                             @RequestParam(required = false) String employeeName) {
        List<ProductOrderVO> list = orderService.getOrderVOList(orderType, customerName, employeeName);
        return Result.success(list);
    }

    // 新增订单
    @PostMapping("/add")
    public Result add(@RequestBody TblProductOrder order) {
        log.info("收到下单请求: {}", order);
        try {
            orderService.addOrder(order);
            return Result.success();
        } catch (Exception e) {
            log.error("下单异常", e);
            return Result.fail(e.getMessage());
        }
    }

    // 修改订单
    @PostMapping("/update")
    public Result update(@RequestBody TblProductOrder order) {
        orderService.updateOrder(order);
        return Result.success();
    }

    // 删除订单
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        orderService.deleteOrder(id);
        return Result.success();
    }

    // 订单详情
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Long id) {
        ProductOrderVO vo = orderService.getOrderDetail(id);
        return Result.success(vo);
    }
}
